﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Math;

namespace Tesla.Graphics {
    /// <summary>
    /// A SpotLight is similar to point with it being located
    /// at a specific point in world space. However, it's light
    /// influence is restricted to a cone (opposed to being omni-directional)
    /// that is controlled by a direction and two angles for falloff to create
    /// a soft edge.
    /// </summary>
    public class SpotLight : Light {
        private Vector3 _position;
        private Vector3 _direction;
        private float _innerAngle;
        private float _outerAngle;
        private LightType _lightType = LightType.Spot;

        /// <summary>
        /// Gets or sets the position of the SpotLight in world space.
        /// 
        /// Default: (0, 0, 0)
        /// </summary>
        public Vector3 Position {
            get {
                return _position;
            }
            set {
                _position = value;
            }
        }

        /// <summary>
        /// Gets or sets the direction of the SpotLight. The value 
        /// is always normalized.
        /// 
        /// Default: (0, 0, -1)
        /// </summary>
        public Vector3 Direction {
            get {
                return _direction;
            }
            set {
                _direction = value;
                _direction.Normalize();
            }
        }

        /// <summary>
        /// Gets or sets the innter cutoff angle for this SpotLight.
        /// This is the extent of the concentration of the light
        /// along the direction (the cone). This should always be smaller than
        /// the outer angle.
        /// 
        /// Default: 45 degrees
        /// </summary>
        public float InnerAngle {
            get {
                return _innerAngle;
            }
            set {
                if(value > 180) {
                    _innerAngle = 180;
                } else {
                    _innerAngle = value;
                }
            }
        }

        /// <summary>
        /// Gest or sets the outer cutoff angle for this SpotLight. This
        /// is used to allow a soft blending from the main concentration
        /// of the light to the area outside of the spotlight's cone.
        /// 
        /// Default: 90 degrees
        /// </summary>
        public float OuterAngle {
            get {
                return _outerAngle;
            }
            set {
                if(value > 180) {
                    _outerAngle = 180;
                } else {
                    _outerAngle = value;
                }
            }
        }

        /// <summary>
        /// Gets the type of light, LightType.Spot.
        /// </summary>
        public override LightType LightType {
            get {
                return _lightType;
            }
        }

        /// <summary>
        /// Creates a new instance of SpotLight, set at the origin
        /// and pointing down the negative Z-axis.
        /// </summary>
        public SpotLight()
            : base("SpotLight") {
                _position = new Vector3();
                _direction = Vector3.Forward;
                _innerAngle = 45.0f;
                _outerAngle = 90.0f;
        }

        /// <summary>
        /// Creates a new SpotLight.
        /// </summary>
        /// <param name="position">Position of the light</param>
        /// <param name="direction">Direction of the light</param>
        /// <param name="innerAngle">Cone's inner angle</param>
        /// <param name="outerAngle">Cone's outer angle</param>
        public SpotLight(Vector3 position, Vector3 direction, float innerAngle, float outerAngle)
            : base("SpotLight") {
                //Set via properties, as they'll do any necessary clamping/normalization
                Position = position;
                Direction = direction;
                InnerAngle = innerAngle;
                OuterAngle = outerAngle;
        }

        /// <summary>
        /// Convienance method for setting the direction of this SpotLight.
        /// </summary>
        /// <param name="x">X component</param>
        /// <param name="y">Y component</param>
        /// <param name="z">Z component</param>
        public void SetDirection(float x, float y, float z) {
            _direction = new Vector3(x, y, z);
            _direction.Normalize();
        }

        /// <summary>
        /// Convienence method for setting the position of this SpotLight
        /// in world space
        /// </summary>
        /// <param name="x">X coordinate</param>
        /// <param name="y">Y coordinate</param>
        /// <param name="z">Z coordinate</param>
        public void SetPosition(float x, float y, float z) {
            _position = new Vector3(x, y, z);
        }

        /// <summary>
        /// Serializes this SpotLight.
        /// </summary>
        /// <param name="output">Output to be written to</param>
        public override void Write(Content.ISavableWriter output) {
            base.Write(output);
            output.Write("LightType", (int) _lightType);
            output.Write("Direction", _direction);
            output.Write("Position", _position);
            output.Write("InnerAngle", _innerAngle);
            output.Write("OuterAngle", _outerAngle);
        }

        /// <summary>
        /// Deserializes this SpotLight.
        /// </summary>
        /// <param name="input">Input to read from</param>
        public override void Read(Content.ISavableReader input) {
            base.Read(input);
            _lightType = (LightType) input.ReadInt();
            _direction = input.ReadVector3();
            _position = input.ReadVector3();
            _innerAngle = input.ReadSingle();
            _outerAngle = input.ReadSingle();
        }
    }
}
